﻿using AutoMapper;
using B.S.Sterilizationcenter.Domain.wjl;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Writer.API.Appliaction.Commends.WJL;
using MediatR;
using SqlSugar;

namespace B.S.Sterilizationcenter.Writer.API.Appliaction.CommandHandlers.WJL
{
    public class QuashCleanwatchCommandHanders : IRequestHandler<QuashCleanwatchCommands, ApiResult<int>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public QuashCleanwatchCommandHanders(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        /// <summary>
        /// 撤销清洗记录
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<int>> Handle(QuashCleanwatchCommands request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<int> result = new ApiResult<int>();

                var res = await db.Queryable<Cleanwatch>().FirstAsync(x => x.CleaningId == request.CleaningId);
                res.IsDelete = true;
                var list = await db.Updateable<Cleanwatch>(res).ExecuteCommandAsync();
                if (list > 0)
                {
                    result.Code = ApiEnums.删除成功;
                    result.Msg = "删除成功";
                }
                else
                {
                    result.Code = ApiEnums.删除失败;
                    result.Msg = "删除失败";
                }
                return result;

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
